
function saveRowSn(rowsn){
   if(document.all.item('rowsn').value.indexOf(rowsn)>-1){
     return;//已記錄過就不再記錄了
   }
   if(document.all.item('rowsn').value==''){
     document.all.item('rowsn').value=rowsn;
   } else{
     document.all.item('rowsn').value=document.all.item('rowsn').value + ',' + rowsn;
   }
}

function delRowSn(rowsn){
  document.all.item('rowsn').value=rowsn;
}

function alert_data(){
   alert(document.all.item('fld_no_g')[5].value);
   checkTheSameFldNo();
}

function on_loading(){
   document.all.item('dataviewtotalcount').value = document.all.item('dataviewtotalcount1').value;
}

function checkTheSameFldNo(obj,rowsn){ // Return Value Data
   var L=document.forms[0].elements.length;
   var strOldFld_no=obj.value;

   // 必需填值
   if (jsf_trim(obj.value)==''){
     alert("欄位名稱必需要有值!!");
   if (jsf_trim(document.all.item('fld_no_old_g')[rowsn].value)==''){
     obj.value=document.all.item('seqno_g')[rowsn].value;
   } else {
     obj.value=document.all.item('fld_no_old_g')[rowsn].value;
   }
  }
   // 檢查是否重複
   for(i=0;i<L;i++){
     if (document.forms[0].elements[i].name=='fld_no_old_g'){
      if(obj.value.toUpperCase()==document.forms[0].elements[i].value.toUpperCase()){
       if (!(document.forms[0].elements[i].value.toUpperCase()==document.all.item('fld_no_old_g')[rowsn].value.toUpperCase())){
        alert("欄位名稱已存在!!");
        obj.value=document.all.item('fld_no_old_g')[rowsn].value;
       }
      }
     }
   }//for
}

function fc_check_to_query(){
// 檢查是否已經有變動，如有變動則不能查詢
 if (document.all.item('prgAction').value!='null'){
   if(document.all.item('rowsn').value!=''){
     alert("資料已有變動，請先存檔後再查詢!!");
     return;
   }
 }
   document.all.item('prgAction').value='query';
   document.forms[0].submit();
}

function fc_add_before_check(){
 if(jsf_trim(document.all.item('dbf_no').value)==''){
   alert("表格名稱不能為空值!!");
   return;
  }
   document.all.item('prgAction').value='add';
   document.forms[0].submit();
}

function fc_save_before_check(){
 var L=document.forms[0].elements.length;
 for(i=0;i<L;i++){
   if (document.forms[0].elements[i].name=='fld_no_g'){
     if(jsf_trim(document.forms[0].elements[i].value)==''){
       alert("欄位名稱不能為空值!!");
       return;
     }
   }
   if(document.forms[0].elements[i].name=='seqno_g'){
     if(jsf_trim(document.forms[0].elements[i].value)=='0000'){
       alert("序號不能為0000,請調整後再儲存!!");
       return;
     }
   }
  }//for
 document.all.item('prgAction').value='saveall';
 document.forms[0].submit();
}
function fc_seqno_reorder(){
  var DatViewCount = document.all.item('dataviewtotalcount').value;
  if (DatViewCount=="1"){document.all.item("seqno_g").value="0001";}
  else{
    for(i=0;i<DatViewCount;i++){
      document.all.item("seqno_g")[i].value = fc_prefix_byzero(i+1,4);
      saveRowSn(i);
    }
  }
}
function fc_pre_generate_sql(){

var DataViewCount = document.all.item('dataviewtotalcount').value;
var objFldNo = "";
var objFldType = "";
var objFldWide = "";
 for(i=0;i<DataViewCount;i++){
   objFldNo = document.all.item('fld_no_g')[i];
   objFldType = document.all.item('fld_kind_g')[i];
   objFldWide = document.all.item('wide_g')[i];
   if(jsf_trim(objFldNo.value)==''){
       alert("欄位名稱不能為空值!!");
       return false;
   }
   if(objFldType[0].selected && objFldWide.value==""){
       alert("請定義 "+objFldNo.value+" 的文字長度!");
       return false;
   }

  }//for
}
function fc_generate_sql(){

  var gSQL = "";
  var dbf_no = document.all.item('dbf_no').value;
  var DatViewCount = document.all.item('dataviewtotalcount').value;
  var fld_kind = "";
  var objKey = "";;
  var objSelect = "";
  var PK_Fld = "";
  if(document.all.item('rowsn').value!=""){
    alert("請先將修改的資料存檔再進行產生SQL的動作!確保產生的SQL與資料庫中的設定相同!");
    document.all.item('gSQL').value="";
    return false;
  }


  gSQL = "CREATE TABLE "+dbf_no+"(";
  gSQL = gSQL +"\n";
  if (DatViewCount=="1"){
    if(document.all.item("fld_no_g").value==""){
      alert("欄位名稱不可為空白!");
      return false;
    }
    if(objSelect.options[0].selected && document.all.item('wide_g').value==""){
      alert("請定義 "+document.all.item("fld_no_g").value+" 的文字長度!");
      return false;
    }
    objSelect = document.all.item('fld_kind_g');
    objKey = document.all.item('is_key_g');
    if(document.all.item("fld_no_g").value!=""){
      gSQL = gSQL + "    " + document.all.item("fld_no_g").value+"    ";
      for(i=0;i<objSelect.options.length;i++){
        if(objSelect.options[i].selected){
          fld_kind = objSelect.options[i].value;
          break;
        }
      }
      if(fld_kind=="C"){
        gSQL = gSQL +"VARCHAR2("+document.all.item('wide_g').value+")";
      }else if(fld_kind=="D"){
        gSQL = gSQL +"DATE";
      }else if(fld_kind=="L"){
        gSQL = gSQL +"LONG RAW";
      }else if(fld_kind=="N"){//數字
        gSQL = gSQL +"NUMBER";
        if(document.all.item('wide_g').value!=""){
          gSQL = gSQL +"(" + document.all.item('wide_g').value;
          if(document.all.item('wide_dec_g').value!=""){
            gSQL = gSQL +"," + document.all.item('wide_dec_g').value + ")";
          }else{
            gSQL = gSQL +",0)";
          }
        }
      }//數字
      //check 是否有填欄位說明
      if(document.all.item('fld_name_g').value!=""){
        if(objKey.options[0].selected){
          gSQL = gSQL +" ,";
        }
        gSQL = gSQL +"    --"+document.all.item('fld_name_g').value;
      }
      if(objKey.options[0].selected){
         gSQL = gSQL +"\n";
         gSQL = gSQL +"    PRIMARY KEY(" + document.all.item("fld_no_g").value + ")";
      }
    }
  }else{
    //2個以上的欄位
    if(fc_pre_generate_sql()==false){
      return false;
    }
    for(j=0;j<DatViewCount;j++){
    objSelect = document.all.item('fld_kind_g')[j];
    objKey = document.all.item('is_key_g')[j];
    if(document.all.item("fld_no_g")[j].value!=""){
      gSQL = gSQL + "    " + document.all.item("fld_no_g")[j].value;
      if(document.all.item("fld_no_g")[j].value.length>8){
        gSQL = gSQL + "\t";
      }else{gSQL = gSQL +"\t\t";}
      for(i=0;i<objSelect.options.length;i++){
        if(objSelect.options[i].selected){
          fld_kind = objSelect.options[i].value;
          break;
        }
      }
      if(fld_kind=="C"){
        gSQL = gSQL +"VARCHAR2("+document.all.item('wide_g')[j].value+")";
      }else if(fld_kind=="D"){
        gSQL = gSQL +"DATE";
      }else if(fld_kind=="L"){
        gSQL = gSQL +"LONG RAW";
      }else if(fld_kind=="N"){//數字
        gSQL = gSQL +"NUMBER";
        if(document.all.item('wide_g')[j].value!=""){
          gSQL = gSQL +"(" + document.all.item('wide_g')[j].value;
          if(document.all.item('wide_dec_g')[j].value!=""){
            gSQL = gSQL +"," + document.all.item('wide_dec_g')[j].value + ")";
          }else{
            gSQL = gSQL +",0)";
          }
        }
      }//數字
      if(objKey.options[0].selected){
         PK_Fld = PK_Fld + document.all.item("fld_no_g")[j].value + ",";
      }
      //check 是否有填欄位說明
      if(j==DatViewCount-1 && PK_Fld==""){
      }else{gSQL = gSQL + ",";}
      if(document.all.item('fld_name_g')[j].value!=""){
        if(fld_kind=="D" || fld_kind=="L"){gSQL = gSQL +"\t\t";}
        else{gSQL = gSQL +"\t";}
        gSQL = gSQL +"--"+document.all.item('fld_name_g')[j].value;
      }
    }
    gSQL = gSQL +"\n";
    }//for i
    if(PK_Fld!=""){
      PK_Fld = PK_Fld.substring(0,PK_Fld.length-1);
      gSQL = gSQL +"    PRIMARY KEY(" + PK_Fld + ")";
      gSQL = gSQL +"\n";
    }
  }
  gSQL = gSQL +")";
  gSQL = gSQL +"\n";
  document.all.item('gSQL').value=gSQL;
}
function fc_copy_sql(obj){
  if(obj.value==""){
    alert("內容為空無法複製!")
    return false;
  }else{
    fc_addClipboard(obj);
    alert("已將內容複製至剪貼簿!")
  }
}
